package epam_java.jsontest;

import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Scanner;

public class JsonTest {
 public static void main(String[] args) {
     System.out.println("Choose greetting language (1-English),(2-Russian)");
     Scanner scanner=new Scanner(System.in);
     ResourceBundle bundle=null;
     while((scanner.hasNextInt())){
         int inext=scanner.nextInt();
         if(inext==1) bundle=ResourceBundle.getBundle("regional_standart", Locale.ENGLISH);
         if(inext==2) bundle= ResourceBundle.getBundle("regional_standart");
         break;
     }
  //   String s="\\[(|\\\"[^\"]*\\\"(\\;
     System.out.println(bundle.getString("HelloMassage"));
     System.out.println(bundle.getString("CurrentDate")+ new Date());
     System.out.println("Press any key");
      while(!scanner.hasNext()){}
    JsonParser parser=new JsonParser("jsontest_utf-8.json", "UTF-8");
        try {
            parser.parse(new UserTest());

            ArrayList<Object> o=parser.getReadObjects();
            if(o.size()>0&&o.get(0) instanceof UserTest){
                UserTest[] list =new UserTest[o.size()];
                for (int i = 0; i < list.length; i++) {
                    list[i]=(UserTest)o.get(i);
                    System.out.println(list[i]);
                }
                    System.out.println("\n-------------------");
                    System.out.println("Среднее дробное любимое число всех пользателей ="+
                            JsonTest.getMiddleDoubleNumber(list));
                    System.out.println("\n-------------------");
                    System.out.println("Среднее целое любимое число всех пользателей ="+
                            JsonTest.getMiddleIntNumber(list));

            }

        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (JsonParserException ex) {
            System.out.println(ex.getMessage());
        } catch (FailureSetterException ex) {
            System.out.println(ex.getMessage());
        } catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }
    }
 public static double getMiddleDoubleNumber(UserTest users[]){
      double count=0;
      double sum=0;
      for(UserTest user:users){
          for(double d:user.getDoublefavouriteNumbers()){
              count++;
              sum+=d;
          }
      }
      return sum/count;
  }
  public static double getMiddleIntNumber(UserTest users[]){
      double count=0;
      int sum=0;
      for(UserTest user:users){
          for(double d:user.getIntfavouriteNumbers()){
              count++;
              sum+=d;
          }
      }
      return sum/count;
  }
}

